package cn.bdqn.controller;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.bdqn.pojo.User;
import cn.bdqn.service.UserService;




@Controller
public class IndexController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value={"/login.html","/index.html"})
	public String show(){
		return "login";
	}
	
	@RequestMapping("/login.do")
	public String login(@RequestParam String userCode ,@RequestParam String userPassword
			,HttpSession session,Model model){
		User user=userService.findByCodeAndPassword(userCode, userPassword);
		
		if(user!=null&&user.getUserPassword().equals(userPassword)){
			session.setAttribute("userlogin", user);
			//重定向
			return "redirect:/main.html";
		}else{
			String error="用户名密码错误";
			model.addAttribute("error", error);
			return "login";
		}
	}
	
	@RequestMapping("/upload.html")
	public String upload(){
		return "upload";
	}
	//单文件	
//	@RequestMapping("/doupload")
//	public String doupload(@RequestParam("userName")String userName,@RequestParam("pic")MultipartFile pic
//			,HttpSession session,Model model){
//		String filePath=session.getServletContext().getRealPath("statics"+File.separator+"images");
//		String picName=pic.getOriginalFilename();
//		File fileTo=new File(filePath, picName);
//		try {
//			pic.transferTo(fileTo);
//			model.addAttribute("userName",userName);
//			model.addAttribute("picName",picName);
//			return "uploadSuccess";
//		}catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			return "error";
//		}
//		
//	}
	
	//多文件	
	@RequestMapping("/doupload")
	public String doupload(@RequestParam("userName")String userName,@RequestParam("pic")MultipartFile[] pic
			,HttpSession session,Model model){
		String filePath=session.getServletContext().getRealPath("statics"+File.separator+"images");
		List<String> picList=new ArrayList<String>();
		for(MultipartFile pi:pic){
			if(!pi.isEmpty()){
				String piName=pi.getOriginalFilename();
				String newPicName=System.currentTimeMillis()+""+Math.round((Math.random()*10000))+piName;
				File fileTo=new File(filePath, newPicName);
				try {
					pi.transferTo(fileTo);
				}  catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					return "error";
				}
				picList.add(newPicName);
			}
		}
		model.addAttribute("userName",userName);
		model.addAttribute("picList",picList);
		return "uploadSuccess";
	}
	
	
	@RequestMapping("/main.html")
	public String back(){
		return "frame";
	}
	
	@RequestMapping("/userlist.html")
	public String userlist(){
		return "userlist";
	}
}
